#include"Tcpconneciton.h"
Tcpconnection::Tcpconnection(int fd)
:_socket(fd)
,_soIO(fd)
{

}
Tcpconnection::~Tcpconnection(){

}
void Tcpconnection::send(const string &msg){
    _soIO.writen(msg.c_str(),msg.size());
}
string Tcpconnection::recv(){
    //写操作
    char buff[10] = {0};
    _soIO.readLine(buff,sizeof(buff));
    return string(buff);
}
void Tcpconnection::setNewConneciton(TcpConnectionCallback _cb){
    _onNewConnection = _cb;
}
void Tcpconnection::setNewMessage(TcpConnectionCallback _cb){
    _onNewMessage = _cb;
}
void Tcpconnection::setNewClose(TcpConnectionCallback _cb){
    _onNewClose = _cb;
}
void Tcpconnection::handlNewConnection(){
    //避免裸体指针的问题
    //_onNewConnection(shared_ptr<Tcpconnection>(this));
    if(_onNewConnection)
    {
        _onNewConnection(shared_from_this());//回调函数的执行
    }
    else
    {
        cout << "_onMessageCb == nullptr" << endl;
    }
}
void Tcpconnection::handlNewMessage(){
    if(_onNewMessage){
        _onNewMessage(shared_from_this());
    }
    else{
        cout<<"_onNewMessage == nullptr "<<endl;
    }

}
void Tcpconnection::handlNeclose(){
    if(_onNewClose){
        _onNewClose(shared_from_this());
    }
    else{
        cout<<"_onNewClose == nullptr "<<endl;
    }
}